package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.DisplayUtil;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**临床路径-标准住院流程-临床路径表单主tab
 * @author Administrator
 *
 */
public class ClinicPathThree extends TabActivity
{
	TabHost tabHost;
	private TabWidget mTabWidget;

	private TextView tv3_current = null;
	private ImageButton tv3_preview = null;
	private TextView tv3_previewtv = null;
	private TextView tv3_currId = null;

	private CustomProgressDialog proDialog;// 加载框
	private ClinicPathThree oThis = this;

	private Intent mIntent = null;
	private Map<String, Object> map_clinic; // 对应科室的病种map
	List<Map<String, Object>> list_clinic = new ArrayList<Map<String, Object>>();// 对应科室的病种list
	String str2_mc, str2_id, str3_mc, str3_id, str4_mc, str4_id;
	private ImageButton mainselected;
	MyApplication myApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.clinicpath_three);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		// 确定view
		findView();

		// 初始化界面数据
		GetData();

		setOnClick();
	}

	private void findView()
	{

		mIntent = getIntent();
		tv3_current = (TextView) this.findViewById(R.id.tv3_current);
		tv3_preview = (ImageButton) this.findViewById(R.id.tv3_preview);
		tv3_previewtv = (TextView) this.findViewById(R.id.tv3_previewtv);
		tv3_currId = (TextView) this.findViewById(R.id.tv3_currId);
		if (mIntent != null)
		{
			str2_mc = mIntent.getStringExtra("str2_mc");
			str2_id = mIntent.getStringExtra("str2_id");
			str3_mc = mIntent.getStringExtra("str3_mc");
			str3_id = mIntent.getStringExtra("str3_id");
			tv3_previewtv.setText(str2_mc);
			tv3_current.setText(str3_mc);
			tv3_currId.setText(str3_id);
		}
		tabHost = getTabHost();
		mTabWidget = (TabWidget) this.findViewById(android.R.id.tabs);
		mainselected = (ImageButton) this.findViewById(R.id.mainselected);
	}

	private void iniTabHost()
	{
		Intent intent1 = new Intent();
		intent1.putExtra("str4_id", list_clinic.get(0).get("id").toString());
		intent1.setClass(this, ClinicPathFour.class);

		Intent intent2 = new Intent();
		intent2.putExtra("str4_id", list_clinic.get(1).get("id").toString());
		intent2.setClass(this, ClinicPathFour.class);

		// 新建一个tab并设置它的，Tag,标题，图标，内容
		tabHost.addTab(tabHost.newTabSpec("标准住院流程").setIndicator("标准住院流程").setContent(intent1));
		tabHost.addTab(tabHost.newTabSpec("临床路径表单").setIndicator("临床路径表单").setContent(intent2));
		tabHost.setCurrentTab(0);// 设置初始选中状态为第一个tab
		// 初始化TabHost
		for (int i = 0; i < mTabWidget.getChildCount(); i++)
		{
			RelativeLayout view = (RelativeLayout) mTabWidget.getChildAt(i);

			TextView text = (TextView) view.findViewById(android.R.id.title);
			text.setTextSize(18);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text.getLayoutParams();
			params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
			params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
			text.setLayoutParams(params);
			text.setGravity(Gravity.CENTER);
			view.setBackgroundColor(getResources().getColor(R.color.color_white));
			if (tabHost.getCurrentTab() == i)
			{
				text.setTextColor(getResources().getColor(R.color.listheadtext_color));

			}
			else
			{
				text.setTextColor(getResources().getColor(R.color.test_color_register));
			}
			int heightPx = DisplayUtil.dip2px(this, 50);
			mTabWidget.getChildAt(i).getLayoutParams().height = heightPx;
		}
		// TabHost切换
		tabHost.setOnTabChangedListener(new OnTabChangeListener()
		{

			public void onTabChanged(String tabId)
			{

				for (int i = 0; i < mTabWidget.getChildCount(); i++)
				{
					View view = mTabWidget.getChildAt(i);
					TextView text = (TextView) view.findViewById(android.R.id.title);
					if (tabHost.getCurrentTab() == i)
					{
						text.setTextColor(getResources().getColor(R.color.listheadtext_color));
					}
					else
					{
						text.setTextColor(getResources().getColor(R.color.test_color_register));
					}
				}
			}

		});
	}

	private void GetData()
	{
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();
	}

	class LoadData implements Runnable
	{

		public void run()
		{

			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			// ls.add(new BasicNameValuePair("userid", myApp.getAccount()));
			ls.add(new BasicNameValuePair("id", tv3_currId.getText().toString()));
			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.CLINICPATH, ls);
			List<Map<String, Object>> tempmain = new ArrayList<Map<String, Object>>();
			Map<String, Object> tempmap = new HashMap<String, Object>();
			if (json != null)
			{
				// 获取科室信息
				// JSONArray jsonArr = new JSONArray(json.getString("tl"));
				JSONArray jsonArr = json.getJSONArray("orglist");
				JSONObject js = null;
				for (int i = 0; i < jsonArr.length(); i++)
				{
					tempmap = new HashMap<String, Object>();
					js = jsonArr.getJSONObject(i);
					tempmap.put("id", js.getString("id"));
					tempmap.put("ljmc", js.getString("ljmc"));
					tempmain.add(tempmap);
				}
				msg.what = 0;
				msg.obj = tempmain;
			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接错误！";
			}

			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					Toast.makeText(oThis, (String) msg.obj, Toast.LENGTH_SHORT).show();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					list_clinic = (List<Map<String, Object>>) msg.obj;
					iniTabHost();
					break;
				default:
					break;
			}
		};
	};

	private void setOnClick()
	{
		// 设置preview监听
		tv3_preview.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{

				ClinicPathThree.this.onBackPressed();
			}
		});
		mainselected.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{

				Intent intent = new Intent();
				intent.setClass(oThis, Mainmean.class);
				startActivity(intent);
				oThis.finish();
			}
		});

	}

}
